package com.juku2024.juku.db.dao

import androidx.lifecycle.LiveData
import androidx.room.*
import com.juku2024.juku.db.entity.SearchHistoryEntity

@Dao
interface SearchKeywordsDao {
    @Query("SELECT * FROM searchKeywords")
    fun queryKeywords(): LiveData<List<SearchHistoryEntity>>?

    @Query("SELECT count(*) FROM searchKeywords")
    fun queryKeywordsCount(): Long?

    //delete from 表名 where 排序字段 in (select 排序字段 from 表名 order by 排序字段 limit 0,n-1);
    @Query("DELETE  FROM searchKeywords where timestamp in(select timestamp FROM searchKeywords ORDER BY  timestamp ASC LIMIT :limit)")
    fun deleteBatch(limit: Int)

    @Query("SELECT keywords FROM searchKeywords ORDER BY timestamp DESC LIMIT :limit")
    fun queryKeywordsLimit(limit: Int): List<String>?

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insert(searchHistoryEntity: SearchHistoryEntity)

    @Update
    fun update(searchHistoryEntity: SearchHistoryEntity)

    @Query("DELETE FROM searchKeywords")
    fun deleteAll()
}